package app.com.rtsplibrary.data;

import java.util.ArrayList;
import java.util.concurrent.LinkedBlockingDeque;

public class VDataUtil {
    public final static int queueSize = 10;
    public LinkedBlockingDeque<H264Data> h264Queue = new LinkedBlockingDeque<>(queueSize);
    private static ArrayList<VDataUtil> dataList = new ArrayList<VDataUtil>();

    public VDataUtil(){
        dataList.add(this);
    }

    public static void putData(byte[] buffer, int type, long ts) {
        for(int i=0; i<dataList.size(); i++) {
            VDataUtil VDataUtil = dataList.get(i);
            if (VDataUtil.h264Queue.size() >= queueSize) {
                VDataUtil.h264Queue.poll();
            }
            H264Data data = new H264Data(buffer, type, ts);
            VDataUtil.h264Queue.add(data);
        }
    }

    public static void putData(H264Data data) {
        for(int i=0; i<dataList.size(); i++) {
            VDataUtil VDataUtil = dataList.get(i);
            if (VDataUtil.h264Queue == null) {
                VDataUtil.h264Queue = new LinkedBlockingDeque<>(queueSize);
            }
            if (VDataUtil.h264Queue.size() >= queueSize) {
                VDataUtil.h264Queue.poll();
            }
            VDataUtil.h264Queue.add(data);
        }
    }

    public LinkedBlockingDeque<H264Data> getH264Queue() {
        return h264Queue;
    }


    public void close(){
        dataList.remove(this);
    }
}
